<template>
	<view class="add-Info-details">
		<view class="cu-form-group" v-if="type == 0 || type == 1">
			<view class="title">{{momNameTitle}}</view>
			<input placeholder="请填写" name="input" v-model="name"></input>
		</view>
		<view class="cu-form-group" @click="$refs['date-time'].show();">
			<view class="title">{{babyTimeTitle}}</view>
			<input placeholder="请选择" name="input" v-model="time"></input>
			<date-time-picker :startYear="type == 2 ? new Date().getFullYear() : 1900 " :endYear="type == 2 ? new Date().getFullYear() +1 : new Date().getFullYear()"
			 ref="date-time" @change='DateChange' :type="type == 1 ? 'datetime-all' :'date'"></date-time-picker>
		</view>
		<view class="cu-form-group" v-if="type == 0 || type == 1">
			<view class="title">手机号码</view>
			<input placeholder="请填写" name="input" v-model="phone"></input>
		</view>
		<view class="but">
			<view class="span" v-if="type == 0 || type == 1">
				<radio class='ztBG margin-left-sm' style="margin-right: 5px;" @click="clickRadio" :checked="radio"></radio>
				<view class="span">
					阅读并同意使用<view class="text-ref" @click="clickFuwu">《服务协议》</view> 和 <view class="text-ref" @click="clickYinsi">《隐私权政策》</view>
				</view>
			</view>
			<view class="sendbut-box">
				<button class="round sendbut" @click="clickAddInfo">完成</button>
			</view>
		</view>
	</view>
</template>

<script>
	import DateTimePicker from '../../../components/bory-dateTimePicker/bory-dateTimePicker.vue'
	import homeApi from '@/config/api/home.js'
	import {
		mapMutations
	} from 'vuex';

	export default {
		components: {
			DateTimePicker
		},
		data() {
			return {
				type: 0, //0 怀孕 ，1 以出生 ，2 老用户-怀孕 ，3 老用户-以出生
				dataList: [],
				momNameTitle: '孕妇姓名',
				babyTimeTitle: '宝宝出生日期',
				name: '',
				time: '',
				phone: '',
				radio: true,
				clickStatus: false
			};
		},
		onLoad(options) {
			this.type = options.type
			if (this.type == 0) {
				this.momNameTitle = '孕妇姓名'
				this.babyTimeTitle = '末次月经'
			} else if (this.type == 1) {
				this.momNameTitle = '母亲姓名'
				this.babyTimeTitle = '宝宝出生日期'
			} else if (this.type == 2) {
				this.babyTimeTitle = '预产期'
				this.momNameTitle = ''
			} else if (this.type == 3) {
				this.babyTimeTitle = '宝宝生日'
				this.momNameTitle = ''
			}
		},
		// 监听导航按钮(如果是左边就返回上一页)
		onNavigationBarButtonTap(e) {
			if (e.float == "left") {
				window.history.back(-1);
			}
		},
		methods: {
			...mapMutations(['login']),
			// 时间选择变化
			DateChange(v) {
				this.time = v
			},
			// 点击隐私服务勾选
			clickRadio() {
				this.radio = !this.radio
			},
			// 保存
			clickAddInfo() {
				// EXPECTANT("怀孕中"),
				// BABY("已出生"),
				if (this.type == 0 || this.type == 1) {
					let phoneReg = /(^1[3456789]\d{9}$)/
					if (this.radio) {
						if (this.name == '') {
							uni.showToast({
								icon: 'none',
								type: 'error',
								title: '请填写姓名'
							})
							return
						} else if (this.time == '') {
							uni.showToast({
								icon: 'none',
								type: 'error',
								title: '请选择时间'
							})
							return
						} else if (this.phone == '') {
							uni.showToast({
								icon: 'none',
								type: 'error',
								title: '请填写手机号码'
							})
							return
						} else if (phoneReg.test(this.phone) == false) {
							uni.showToast({
								icon: 'none',
								type: 'error',
								title: '请填写正确的手机号码'
							})
							return
						}
					} else {
						uni.showToast({
							icon: 'none',
							type: 'error',
							title: '请同意隐私条款'
						})
						return
					}

				}

				let that = this
				if (that.clickStatus == false) {
					that.clickStatus = true
					if (that.type == 0 || that.type == 1) {
						let data = {
							'id': uni.getStorageSync('userInfo').id,
							"birthTime": that.type == 1 || that.type == 3 ? that.time : '',
							"dueDate": that.type == 2 ? that.time : '',
							"menstruation": that.type == 0 ? that.time : '',
							"momName": that.name,
							"phone": that.phone,
							"userStatus": that.type == 0 || that.type == 2 ? 'EXPECTANT' : 'BABY'
						}
						homeApi.addUserInfo(data).then(res => {
							uni.login({
								success: function(loginRes) {
									if (loginRes) {
										//获取用户信息
										uni.getUserInfo({
											success: function(infoRes) {
												homeApi.login({
													code: loginRes.code, //临时登录凭证
													rawData: infoRes.rawData, //用户非敏感信息
													signature: infoRes.signature, //签名
													encryptedData: infoRes.encryptedData, //用户敏感信息
													iv: infoRes.iv //解密算法的向量
												}).then(lres => {
													// 设置登陆状态
													that.login(lres)
													// 设置选项卡信息
													uni.setStorage({
														key: 'cardInfo',
														data: res
													})
													//缓存用户状态,是否为新用户
													uni.setStorage({
														key: 'userNewStatus',
														data: lres.login
													})
													uni.switchTab({
														url: '../index'
													})
												})
											}
										});
									}
								}
							});

						})
					} else {
						let data = {
							'id': uni.getStorageSync('userInfo').id,
							"birthTime": that.type == 1 || that.type == 3 ? that.time : '',
							"dueDate": that.type == 2 ? that.time : '',
							'userStatus': that.type == 0 || that.type == 2 ? 'EXPECTANT' : 'BABY',
						}
						homeApi.updUserStatus(data).then(res => {
							uni.login({
								success: function(loginRes) {
									if (loginRes) {
										//获取用户信息
										uni.getUserInfo({
											success: function(infoRes) {
												homeApi.login({
													code: loginRes.code, //临时登录凭证
													rawData: infoRes.rawData, //用户非敏感信息
													signature: infoRes.signature, //签名
													encryptedData: infoRes.encryptedData, //用户敏感信息
													iv: infoRes.iv //解密算法的向量
												}).then(lres => {
													// 设置登陆状态
													that.login(lres)
													// 设置选项卡信息
													uni.setStorage({
														key: 'cardInfo',
														data: res
													})
													//缓存用户状态,是否为新用户
													uni.setStorage({
														key: 'userNewStatus',
														data: lres.login
													})
													uni.switchTab({
														url: '../index'
													})
												})
											}
										});
									}
								}
							});

						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						type: 'error',
						title: '请不要重复点击'
					})
					return
				}
			}
		}
	}
</script>

<style lang="less">
	page {
		height: 100%;
	}

	.flex {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.add-Info-details {
		input {
			text-align: right;
		}

		.but {
			margin-top: 8vh;
			width: 100vw;
			display: flex;
			align-items: center;
			flex-direction: column;

			.span {
				display: flex;
				align-items: center;

				radio {
					transform: scale(0.7);
				}

				.text-ref {
					color: #0081FF;
					border-bottom: 1px solid #276af3;
				}
			}

			.sendbut-box {
				padding: 4vh 10vw;
				width: 100vw;

				.sendbut {
					background: #276af3;
					color: #fff;
				}
			}


		}
	}
</style>
